package com.cenwangkeji.resident.receiver;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.cenwangkeji.resident.MyApp;
import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.ui.activity.MessageDetailActivity;
import com.google.gson.Gson;
import com.lws.basic.utils.common.MyLogger;

import java.io.File;
import java.util.Random;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

import static android.app.Notification.VISIBILITY_SECRET;
import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * @松 on 2019/6/24 09:34
 * 极光广播
 */
public class PushReceiver extends JPushMessageReceiver {
    public int id = 0;

    @Override//自定义消息
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
        MyLogger.i(customMessage.message);
        MyLogger.i(customMessage.extra);
        int notifyId = new Random().nextInt(5000);
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Intent resultIntent = new Intent(context, MessageDetailActivity.class);
        resultIntent.putExtra("date", customMessage.extra);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                context, notifyId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new NotificationCompat.Builder(context,"com.cenwangkeji.resident").setContentTitle(customMessage.title)
                .setContentText(customMessage.message).setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_small_logo)
                .setLargeIcon(BitmapFactory.decodeResource(MyApp.getContext().getResources(), R.mipmap.ic_login_logo))
                .setContentIntent(resultPendingIntent).setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
               .build();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("com.cenwangkeji.resident", "居民端通知", NotificationManager.IMPORTANCE_HIGH);

            channel.setSound(Uri.fromFile( new File("/system/media/audio/ringtones/Orion.ogg")),
                    new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build());
            //是否绕过请勿打扰模式
          //  channel.canBypassDnd();
            //闪光灯
            channel.enableLights(true);
            //锁屏显示通知
            channel.setLockscreenVisibility(VISIBILITY_SECRET);
            //闪关灯的灯光颜色
           // channel.setLightColor(Color.RED);
            //桌面launcher的消息角标
            channel.canShowBadge();
            //是否允许震动
            channel.enableVibration(true);
            //获取系统通知响铃声音的配置
            channel.getAudioAttributes();
            //获取通知取到组
           // channel.getGroup();
            //设置可绕过  请勿打扰模式
            channel.setBypassDnd(true);
            //设置震动模式
            channel.setVibrationPattern(new long[]{100, 100, 200});
            //是否会有灯光
            channel.shouldShowLights();

            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
        }
        //int notifyId = new Random().nextInt(5000);
        manager.notify(notifyId, notification);

    }

    @Override//点开推送
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
    }

    @Override //收到推送
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);
    }
}
